package com.hsy.linemoney.apiservice;

import com.deparse.dpframe.tools.utils.DPGsonUtil;
import com.hsy.linemoney.R;
import com.hsy.linemoney.requests.RequestTask;
import com.hsy.linemoney.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 兑换计算相关 - 美元换人民币/人民币换美元
 * Created by MartinKent on 2016/9/12.
 */
public class CalculateService extends BasicService<JSONArray> {

    public void calculate(final String sourece,
                          final String target,
                          final String sendAmount,
                          final String receivedAmount,
                          final String serviceTypeCd,
                          final String userDefineRate,
                          final Callback<CalculateResult> callback) {
        final JSONObject params = ParamsMaker.newInstance().add("source", sourece)
                .add("target", target)
                .add("sendAmount", sendAmount)
                .add("receivedAmount", receivedAmount)
                .add("serviceTypeCd", serviceTypeCd)
                .add("userDefineRate", userDefineRate)
                .get();
        final String api = "/LmCurrencyConverter-portlet.fxrate/calculate";
        invoke(0, api, params, new Callback<JSONArray>() {
            @Override
            public void onSuccess(int what, JSONArray result) {
                try {
                    if (null != callback) {
                        callback.onSuccess(what, DPGsonUtil.fromJson(result.get(0).toString(), CalculateResult.class));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    if (null != callback) {
                        callback.onError(what, 0, e.getLocalizedMessage(), e);
                    }
                }
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                if (e instanceof RequestTask.StatusCodeException) {
                    ToastUtil.showToast(msg);
                } else {
                    ToastUtil.showToast(R.string.txt_network_failure);
                }
                if (null != callback) {
                    callback.onError(what, 0, msg, e);
                }
            }
        });
    }

    public static class CalculateResult extends BasicGsonData {
        private Status status;

        private Data data;

        public void setStatus(Status status) {
            this.status = status;
        }

        public Status getStatus() {
            return this.status;
        }

        public void setData(Data data) {
            this.data = data;
        }

        public Data getData() {
            return this.data;
        }

        public static class Data {
            private LmData lmData;

            private BankData bankData;

            public void setLmData(LmData lmData) {
                this.lmData = lmData;
            }

            public LmData getLmData() {
                return this.lmData;
            }

            public void setBankData(BankData bankData) {
                this.bankData = bankData;
            }

            public BankData getBankData() {
                return this.bankData;
            }

        }

        public static class BankData {
            private String bankPayOut;

            private String bankRate;

            private String bankTransferFee;

            private String bankPayIn;

            public void setBankPayOut(String bankPayOut) {
                this.bankPayOut = bankPayOut;
            }

            public String getBankPayOut() {
                return this.bankPayOut;
            }

            public void setBankRate(String bankRate) {
                this.bankRate = bankRate;
            }

            public String getBankRate() {
                return this.bankRate;
            }

            public void setBankTransferFee(String bankTransferFee) {
                this.bankTransferFee = bankTransferFee;
            }

            public String getBankTransferFee() {
                return this.bankTransferFee;
            }

            public void setBankPayIn(String bankPayIn) {
                this.bankPayIn = bankPayIn;
            }

            public String getBankPayIn() {
                return this.bankPayIn;
            }

        }
    }

    public static class LmData {
        private String linemoneyTransferFee;

        private boolean isUsingUserDefineRate;

        private String linemoneyRate;

        private String linemoneyPayOut;

        private String serviceType;

        private String linemoneyPayIn;

        public void setLinemoneyTransferFee(String linemoneyTransferFee) {
            this.linemoneyTransferFee = linemoneyTransferFee;
        }

        public String getLinemoneyTransferFee() {
            return this.linemoneyTransferFee;
        }

        public void setIsUsingUserDefineRate(boolean isUsingUserDefineRate) {
            this.isUsingUserDefineRate = isUsingUserDefineRate;
        }

        public boolean getIsUsingUserDefineRate() {
            return this.isUsingUserDefineRate;
        }

        public void setLinemoneyRate(String linemoneyRate) {
            this.linemoneyRate = linemoneyRate;
        }

        public String getLinemoneyRate() {
            return this.linemoneyRate;
        }

        public void setLinemoneyPayOut(String linemoneyPayOut) {
            this.linemoneyPayOut = linemoneyPayOut;
        }

        public String getLinemoneyPayOut() {
            return this.linemoneyPayOut;
        }

        public void setServiceType(String serviceType) {
            this.serviceType = serviceType;
        }

        public String getServiceType() {
            return this.serviceType;
        }

        public void setLinemoneyPayIn(String linemoneyPayIn) {
            this.linemoneyPayIn = linemoneyPayIn;
        }

        public String getLinemoneyPayIn() {
            return this.linemoneyPayIn;
        }

    }

    public static class Status {
        private String statusCode;

        public void setStatusCode(String statusCode) {
            this.statusCode = statusCode;
        }

        public String getStatusCode() {
            return this.statusCode;
        }

    }
}
